﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using DAL;

namespace 包覆门.GlassManager
{
    public partial class GlassEditing : Form
    {
        public GlassEditing()
        {
            InitializeComponent();
        }
        public string EditingId { get; set; }
        public bool IsNew { get; set; }
        private GlassDAL dal = new GlassDAL();
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            SpecialGlass sg = new SpecialGlass();
            sg.GlassType = tbType.Text;
            sg.GlassPrice = tbPrice.Text;
            foreach (RadioButton rb in this.gbSpecial.Controls) {
                if (rb.Checked) {
                    sg.IsSpecial = Convert.ToInt32(rb.Tag);
                    break;
                }
            }
            if (IsNew)
            {
                int result = dal.SaveData(sg);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else 
            {
                sg.Gid = Convert.ToInt32(EditingId);
                int result = dal.UpdateData(sg);
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
        //jiazaishuju 
        private void GlassEditing_Load(object sender, EventArgs e)
        {
            if (IsNew)
            {

            }
            else {
                SpecialGlass sg = dal.GetByGid(EditingId);
                this.tbPrice.Text = sg.GlassPrice;
                this.tbType.Text = sg.GlassType;
                foreach (RadioButton rb in this.gbSpecial.Controls) {
                    if (rb.Tag.Equals(sg.IsSpecial.ToString())) {
                        rb.Checked = true;
                        break;
                    }
                }
            }
        }
        

    }
}
